package com.digua.atools.tools;

import com.blankj.utilcode.util.StringUtils;
import com.digua.atools.tools.listener.DataToDataListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author:digua
 * @dat:2021/8/23
 * @description:枚举工具类
 */
public class EnumUtil {
    /**
     * 将枚举转成List
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> List<T> toList(Class<T> t) {
        return Arrays.asList(t.getEnumConstants());
    }

    /**
     * 将枚举转成指定类型的List
     *
     * @param t
     * @param listener
     * @param <T1>
     * @param <T2>
     * @return
     */
    public static <T1 extends Enum<T1>, T2> List<T2> toList(Class<T1> t, DataToDataListener<T1, T2> listener) {
        if (t == null || listener == null) {
            return null;
        }
        ArrayList<T1> list = new ArrayList<>(Arrays.asList(t.getEnumConstants()));
        return com.digua.atools.tools.CollUtil.listToList(list, listener);
    }

    /**
     * 获取枚举值
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>, V> T getValue(Class<T> t, V v, DataToDataListener<T, V>... listener) {
        return getNoNullValue(t, v, null, listener);
    }

    /**
     * 获取枚举值
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>, V> T getNoNullValue(Class<T> t, V v, T defVal, DataToDataListener<T, V>... listener) {
        if (v == null) {
            return defVal;
        }
        if (com.digua.atools.tools.ObjUtil.eq(t.getSimpleName(), v.getClass().getSimpleName())) {
            return (T) v;
        }
        T[] ts = t.getEnumConstants();
        if (listener != null && listener.length > 0) {
            for (T t1 : ts) {
                V v1 = listener[0].d2d(t1);
                if (com.digua.atools.tools.ObjUtil.eq(v, v1)) {
                    return t1;
                }
            }
        } else {
            if (v instanceof Integer) {
                int index = (Integer) v;
                return index < 0 || index > ts.length - 1 ? defVal : ts[index];
            }
            if (v instanceof String) {
                for (T t1 : ts) {
                    if (StringUtils.equals(t1.name(), v.toString())) {
                        return t1;
                    }
                }
            }
        }
        return defVal;
    }

    /**
     * 获取数据在枚举中的ordinal
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>, V> Integer getValueIndex(Class<T> t, V v, DataToDataListener<T, V> listener) {
        return getValueIndex(t, v, null, listener);
    }

    /**
     * 获取数据在枚举中的ordinal
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>, V> Integer getValueIndex(Class<T> t, V v, T defVal, DataToDataListener<T, V> listener) {
        T val = getNoNullValue(t, v, defVal, listener);
        return val == null ? null : val.ordinal();
    }

    /**
     * 判断传入参数中是否有包含的
     * @param data
     * @param vals 传入参数
     * @param <T>
     * @return
     */
    public static <T extends Enum<T>> boolean eqHas(T data, T... vals) {
        if (data == null || vals == null || vals.length <= 0) {
            return false;
        }
        for (T val : vals) {
            if (data == val) {
                return true;
            }
        }
        return false;
    }
}
